namespace Brjnk.CoordinateSystem2D
{
    using System;
    using System.Linq;
    using System.Windows;

    /// <summary>
    /// </summary>
    public class LogAxis : AxisDefinition
    {
        public LogAxis() { }

        public override GetAxisLabelsResult GetAxisLabels(double from, double to, double minSpacing)
        {
            var r = new CalculateStepsResult();
            r.stepBig = (int)Math.Ceiling(minSpacing);
            r.stepSmall = r.stepBig;
            r.formatterBig = new Func<double, string>(d => TransformBack(d).ToString());
            r.LargeZero = true;
            r.Reverse = true;
            return new GetAxisLabelsResult(this.Caption, this.GenerateLabels(from, to, r).ToArray());
        }

        public override string GetLabelTextFull(double value)
        {
            return value.ToString();
        }

        public override double Transform(double value)
        {
            if (value <= 0) return double.NegativeInfinity;
            return Math.Log(value, LogBase);
        }

        public override double TransformBack(double value)
        {
            if (double.IsNegativeInfinity(value)) return 0;
            return Math.Pow(LogBase, value);
        }

        protected override System.Windows.Freezable CreateInstanceCore()
        {
            return new LogAxis();
        }

        public double LogBase
        {
            get { return (double)GetValue(LogBaseProperty); }
            set { SetValue(LogBaseProperty, value); }
        }

        public static readonly DependencyProperty LogBaseProperty =
            DependencyProperty.Register("LogBase", typeof(double), typeof(LogAxis), new UIPropertyMetadata(10.0, AxisChanged));
    }
}